import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Set;

/***
 * 页面属性和切换相关的
 */


public class Demo2 {
    private EdgeDriver open() {
        // 驱动管理程序下载驱动
        WebDriverManager.edgedriver().setup();

        // 设置器设置属性
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");

        // 生成一个驱动页面的句柄
        EdgeDriver driver = new EdgeDriver();

        driver.get("https://www.baidu.com/");
        return driver;
    }

    private void close(EdgeDriver edgeDriver) {
        // 关闭页面
        edgeDriver.quit();
    }

    /**
     * 获取页面的属性
     */
    public void test_01() {
        EdgeDriver driver = open();

        System.out.println(driver.getCurrentUrl());

        System.out.println(driver.getTitle());

        close(driver);
    }

    /**
     * 页面切换
     */

    public void test_02() throws InterruptedException {
        EdgeDriver driver = open();

        /**
         * 1.点击百度新闻
         */
        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).click();

        /**
         * 2.切换driver页面
         */
        Thread.sleep(3000);
        Set<String> windowHandles = driver.getWindowHandles();
        for (String w:windowHandles) {
            if (!w.equals(driver.getWindowHandle())) {
                // 不一致就切换页面
                driver.switchTo().window(w);
            }
        }

        /**
         * 3.然后点击新闻头条
         */
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#pane-news > div > ul > li.hdline0 > strong > a")).click();

        Thread.sleep(3000);
        close(driver);
    }


    /**
     *  自动化程序： 屏幕截图
     */

    public void test03() throws IOException {
        EdgeDriver driver = open();

        // 屏幕截图
        File screenFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        // 将得到的屏幕截图拷贝到新的文件中
        FileUtils.copyFile(screenFile,new File("baidu.png"));


        close(driver);
    }

    /**
     * 多个页面屏幕截图
     */


    public void test_04() throws IOException, InterruptedException {
        EdgeDriver driver = open();
        takePhoto(driver);

        // 点击一个事件
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
//        切换页面
        for (String  w:driver.getWindowHandles()) {
            if (!w.equals(driver.getWindowHandle())) {
                driver.switchTo().window(w);
            }
        }

        takePhoto(driver);

        close(driver);
    }

    private void takePhoto(EdgeDriver driver) throws IOException {
        SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd-");
        SimpleDateFormat time = new SimpleDateFormat("HH-mm-ss-SS-");
        String dayDate = day.format(System.currentTimeMillis());
        String TimeDate = time.format(System.currentTimeMillis());

//        用时间来区分文件的不同
        String newFile = "./src/pic/" + dayDate + TimeDate + "test.png";

//        得到截屏文件
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // 拷贝文件
        FileUtils.copyFile(srcFile,new File(newFile));
    }



}
